//
//  HomeCenterScrollView.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/8/26.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "HomeCenterScrollView.h"
#import "Scrollerview.h"

@interface HomeCenterScrollView () <UIScrollViewDelegate, scrollCycleDelegate>

//@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) Scrollerview *scrollview;

@property(nonatomic,strong) NSMutableArray *cardImageViews;
@property(nonatomic,strong) NSMutableArray *cardImagebtnTitleViews;

@property(nonatomic,strong) NSMutableArray *imagesArray;
@property(nonatomic,strong) NSMutableArray *imagesBtnTitleArray;

@property(nonatomic,assign) NSInteger imagesTotalCount;
@property(nonatomic,assign) NSInteger cardViewMixCount;


@property (nonatomic, strong) UIButton *btnTitle;
@property (nonatomic, strong) NSMutableArray *modelArr;
@property (nonatomic, strong) UILabel *lbTopTitle;
@property (nonatomic, strong) UILabel *lbDetailTitle;
@property (nonatomic, strong) UILabel *lbDetail;


@end

#define cardContentViewCount 7 //显示循环用的view个数

@implementation HomeCenterScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(scrollEnd:)
                                                     name:@"HomeCardScrollIndex"
                                                   object:nil];
    }
    return self;
}

- (void)updateUIWith:(NSArray <HomeShopSHModel *> *)modelArr title:(NSString *)title {
    
    if (IsNotNilArr(modelArr)) {
        self.modelArr = (NSMutableArray *)modelArr;
        for (int i = 0; i < modelArr.count; i++) {
            HomeShopSHModel *model = modelArr[i];
            [self.imagesArray addObject:DSStringValue(model.imgPath)];
            [self.imagesBtnTitleArray addObject:DSStringValue(model.tag)];
        }
        
        [self initUI];
        HomeShopSHModel *model = modelArr[0];
        self.lbDetailTitle.text = DSStringValue(model.title);
        self.lbDetail.text = DSStringValue(model.presentation);
        self.lbTopTitle.text = DSStringValue(title);
    }
}

- (void)initUI {
    
    [self addSubview:self.lbTopTitle];
    [self addSubview:self.scrollview];
    [self addSubview:self.lbDetailTitle];
    [self addSubview:self.lbDetail];
}

- (UIImageView *)imageViewAtIndex:(NSUInteger)index {
    UIImageView *view;
    while(_cardImageViews.count <= index){
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CardW, CardH)];
        view.userInteractionEnabled = YES;
        [_cardImageViews addObject:view];
    }
    return _cardImageViews[index];
}

- (UIButton *)buttonAtIndex:(NSUInteger)index {
    UIButton *btnTitle;
    while(_cardImagebtnTitleViews.count <= index) {
        btnTitle = [[UIButton alloc] initWithFrame:CGRectMake(12, CardH - 18 - 14, 52, 18)];
        btnTitle.layer.cornerRadius = 8.3;
        btnTitle.alpha = 0.8;
        btnTitle.backgroundColor = UIColorFromRGB(0x030303);
        btnTitle.titleLabel.font = FONT_BOLD(11);
        [_cardImagebtnTitleViews addObject:btnTitle];
    }
    return _cardImagebtnTitleViews[index];
}

static NSInteger safeIndexValue(NSInteger totalCount, NSInteger index) {
    if (totalCount <= 0) return 0;
    if (index < 0) {
        index = index %totalCount;
        if (index < 0) {
            index= (totalCount+index);
        }
    } else if (index>=totalCount) {
        index = (index%totalCount);
    }
    return index;
}

#pragma mark - scrollCycleDelegate
- (UIView *)imageViewAtIndex:(NSInteger)index currentIndex:(NSInteger)currentIndex {
    UIImageView *imageView = [self imageViewAtIndex:index];
    index = safeIndexValue(_imagesTotalCount, index);
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesArray[index]] placeholderImage:LHYPlaceholderImage];
    return imageView;
}

- (UIButton *)buttonAtIndex:(NSInteger)index currentIndex:(NSInteger)currentIndex {
    UIButton *btn = [self buttonAtIndex:index];
    index = safeIndexValue(_imagesTotalCount, index);
    [btn setTitle:self.imagesBtnTitleArray[index] forState:UIControlStateNormal];
    
    
    return btn;
}

- (void)actionBtn:(UIButton *)sender {
    HomeShopSHModel *model = self.modelArr[sender.tag - 20];
    if (self.block) {
        self.block(model.type.integerValue, [NSString stringWithFormat:@"%@", model.linkPath], [NSString stringWithFormat:@"%@", model.appId], [NSString stringWithFormat:@"%@", model.appPath]);
    }
}

//#pragma mark - scrollView代理方法 -
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    //拖拽结束,如果没有减速操作，直接同步pageControl
//    if(!decelerate){
//        [self updateUI];
//    }
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    //减速结束，同步pageControl
//    [self updateUI];
//}
//
//- (void)updateUI {
//    //根据scrollView的x偏移量，计算当前scroll停留在哪一页。
//
//    CGFloat x = _scrollview.contentOffset.x;
//    NSInteger index = 0;
//
//    if (x < 130 / 2) {
//        index = 0;
//    } else {
//        index = _scrollview.contentOffset.x / 130 + 1;
//        if (index >= self.modelArr.count) {
//            index--;
//        }
//    }
//    NSLog(@"%ld", index);
//
//    HomeShopSHModel *model = self.modelArr[index];
//    self.lbDetailTitle.text = model.title;
//    self.lbDetail.text = model.presentation;
//}

- (void)scrollEnd:(NSNotification *)noti {
    NSInteger index = [noti.userInfo[@"index"] integerValue];
    
    HomeShopSHModel *model = self.modelArr[index];
    self.lbDetailTitle.text = model.title;
    self.lbDetail.text = model.presentation;
}

- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [[NSMutableArray alloc] init];
    }
    return _modelArr;
}

- (UILabel *)lbTopTitle {
    if (!_lbTopTitle) {
        _lbTopTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, 24, SCREEN_WIDTH - 32, 31)];
        _lbTopTitle.font = FONT_BOLD(22);
    }
    return _lbTopTitle;
}

- (UILabel *)lbDetailTitle {
    if (!_lbDetailTitle) {
        
        _lbDetailTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, GetViewYH(self.scrollview) + 12, SCREEN_WIDTH - 32, 23)];
        _lbDetailTitle.font = FONT_BOLD(16);
    }
    return _lbDetailTitle;
}

- (UILabel *)lbDetail {
    if (!_lbDetail) {
        
        _lbDetail = [[UILabel alloc] initWithFrame:CGRectMake(16, GetViewYH(self.lbDetailTitle) + 12, SCREEN_WIDTH - 32, 37)];
        _lbDetail.numberOfLines = 2;
        _lbDetail.textColor = UIColorFromRGB(0x555555);
        _lbDetail.font = FONT(13);
    }
    return _lbDetail;
}

- (Scrollerview *)scrollview {
    if (!_scrollview) {
        
        _scrollview = [[Scrollerview alloc] initWithFrame:CGRectMake(0, 55 + 16, SCREEN_WIDTH, 182)];
        _scrollview.dataSourceDelegate = self;
        _imagesTotalCount = _imagesArray.count;
        _cardImageViews = [NSMutableArray new];
        _cardImagebtnTitleViews = [[NSMutableArray alloc] init];
        
        NSInteger cardViewMixCount = _imagesTotalCount<cardContentViewCount?((cardContentViewCount/_imagesTotalCount+1)*_imagesTotalCount):_imagesTotalCount;
        [_scrollview setCount:cardViewMixCount withCurrentIndex:0 dataCount:_imagesTotalCount];
        
    }
    return _scrollview;
}

//- (UIScrollView *)scrollView {
//    if (!_scrollView) {
//        // 2.初始化、配置scrollView
//        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 55 + 16, SCREEN_WIDTH, 182)];
//        _scrollView.contentSize = CGSizeMake(120 * (self.modelArr.count + 2), 182);
//        _scrollView.delegate = self;
//        _scrollView.showsHorizontalScrollIndicator = NO;
//
//        for (int i = 0; i < self.modelArr.count; i++) {
//            HomeShopSHModel *model = self.modelArr[i];
//
//            BaseButton *btn = [[BaseButton alloc] initWithFrame:CGRectMake(16 + i * 130, 5.5, 120, 161)];
//            [btn sd_setImageWithURL:[NSURL URLWithString:model.imgPath] forState:UIControlStateNormal placeholderImage:LHYPlaceholderImage];
//
//            btn.layer.shadowColor = [UIColor colorWithRed:11/255.0 green:48/255.0 blue:132/255.0 alpha:0.36].CGColor;
//            btn.layer.shadowRadius = 5.f;
//            btn.layer.shadowOffset = CGSizeMake(2.f, 2.f);
//            btn.layer.shadowOpacity = 1.f;
//
//            [_scrollView addSubview:btn];
//
//            [btn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
//            btn.tag = 20 + i;
//            self.btnTitle = [[UIButton alloc] initWithFrame:CGRectMake(12, btn.frame.size.height - 12 - 18, 52, 18)];
//            self.btnTitle.layer.cornerRadius = 8.3;
//            self.btnTitle.alpha = 0.8;
//            self.btnTitle.backgroundColor = UIColorFromRGB(0x030303);
//            [self.btnTitle setTitle:model.title forState:UIControlStateNormal];
//            self.btnTitle.titleLabel.font = FONT_BOLD(11);
//            [btn addSubview:self.btnTitle];
//        }
//    }
//    return _scrollView;
//}

- (NSMutableArray *)imagesArray {
    if (!_imagesArray) {
        _imagesArray = [[NSMutableArray alloc] init];
    }
    return _imagesArray;
}

- (NSMutableArray *)imagesBtnTitleArray {
    if (!_imagesBtnTitleArray) {
        _imagesBtnTitleArray = [[NSMutableArray alloc] init];
    }
    return _imagesBtnTitleArray;
}

@end
